import 'package:bruno/bruno.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'package:get/get.dart';

import '../../../../../app/routes/app_pages.dart';
import '../../../../data/models/doctor_list_res.dart';
import '../../../../data/models/get_article_res.dart';
import '../controllers/home_page_controller.dart';

class HomePageView extends GetView<HomePageController> {
  const HomePageView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey[100],
      body: SafeArea(
        child: Column(
          children: [
            // Search Bar
            Padding(
              padding: EdgeInsets.all(16.w),
              child: Container(
                height: 40.h,
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(20.r),
                ),
                child: Row(
                  children: [
                    Padding(
                      padding: EdgeInsets.symmetric(horizontal: 12.w),
                      child: Icon(Icons.search, color: Colors.grey),
                    ),
                    Text(
                      '搜索症状、医生、科室',
                      style: TextStyle(
                        color: Colors.grey,
                        fontSize: 14.sp,
                      ),
                    ),
                    const Spacer(),
                    Padding(
                      padding: EdgeInsets.only(right: 12.w),
                      child: Icon(Icons.mic, color: Colors.grey),
                    ),
                  ],
                ),
              ),
            ),

            Expanded(
              child: ListView(
                shrinkWrap: true,
                padding: EdgeInsets.symmetric(horizontal: 16.w),
                children: [
                  // Main Cards
                  Row(
                    children: [
                      Expanded(
                        child: _buildMainCard('AI 问诊', '智能诊断 快速解答',
                            Colors.blue[600]!, Icons.android, onclick: () {
                          BrnToast.show("当前功能暂未实现,\n请期待后续开发~",
                              Get.context as BuildContext);
                        }),
                      ),
                      SizedBox(width: 12.w),
                      Expanded(
                        child: _buildMainCard(
                            '医生问诊',
                            '20+ 名医生在线',
                            Colors.green[600]!,
                            Icons.local_hospital, onclick: () {
                          Get.toNamed(
                            Routes.DOCTOR,
                          );
                        }),
                      ),
                    ],
                  ),

                  // Common Functions
                  Padding(
                    padding: EdgeInsets.symmetric(vertical: 24.h),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          '常用功能',
                          style: TextStyle(
                            fontSize: 16.sp,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                        SizedBox(height: 16.h),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                          children: [
                            _buildFunctionItem(Icons.article, '健康档案'),
                            _buildFunctionItem(Icons.calendar_today, '预约挂号'),
                            _buildFunctionItem(Icons.medical_services, '用药提醒'),
                            _buildFunctionItem(Icons.fitness_center, '体检预约'),
                          ],
                        ),
                      ],
                    ),
                  ),

                  // Recommended Doctors
                  Obx(
                    () => Column(
                      key: Key('doctorList'),
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          '推荐医生',
                          style: TextStyle(
                            fontSize: 16.sp,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                        SizedBox(height: 16.h),
                        if (controller.doctorList.isNotEmpty)
                          ListView.builder(
                            shrinkWrap: true,
                            physics: NeverScrollableScrollPhysics(),
                            itemCount: controller.doctorList.length,
                            itemBuilder: (context, index) {
                              return _buildDoctorCard(
                                  controller.doctorList[index]);
                            },
                          )
                      ],
                    ),
                  ),
                  Obx(
                    () => Column(
                      key: Key('articleList'),
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(
                              '医疗咨询',
                              style: TextStyle(
                                fontSize: 16.sp,
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                            TextButton(
                              onPressed: () {
                                Get.toNamed(Routes.NEWS_LIST);
                              },
                              child: Text('更多',style: TextStyle(color: Color(0xFF666666)),),
                            ),
                          ],
                        ),
                        SizedBox(height: 16.h),
                        if (controller.articleList.isNotEmpty)
                          ListView.builder(
                            shrinkWrap: true,
                            physics: NeverScrollableScrollPhysics(),
                            itemCount: controller.articleList.length,
                            itemBuilder: (context, index) {
                              return _buildArticleCard(
                                  controller.articleList[index]);
                            },
                          )
                      ],
                    ),
                  ),
                  SizedBox(height: 24.h)
                ],
              ),
            ),

            // Bottom Navigation Bar
          ],
        ),
      ),
    );
  }

  Widget _buildMainCard(
      String title, String subtitle, Color color, IconData icon,
      {Function? onclick}) {
    return GestureDetector(
      onTap: () {
        onclick?.call();
      },
      child: Container(
        height: 120.w,
        decoration: BoxDecoration(
          color: color,
          borderRadius: BorderRadius.circular(12.r),
        ),
        child: Stack(
          children: [
            Padding(
              padding: EdgeInsets.all(16.w),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Icon(icon, color: Colors.white, size: 32.w),
                  SizedBox(height: 8.w),
                  Text(
                    title,
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 18.sp,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  SizedBox(height: 4.w),
                  Text(
                    subtitle,
                    style: TextStyle(
                      color: Colors.white.withOpacity(0.8),
                      fontSize: 12.sp,
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildFunctionItem(IconData icon, String label) {
    return Column(
      children: [
        Container(
          width: 48.w,
          height: 48.w,
          decoration: BoxDecoration(
            color: Colors.blue[100],
            borderRadius: BorderRadius.circular(24.r),
          ),
          child: Icon(icon, color: Colors.blue[600]),
        ),
        SizedBox(height: 8.h),
        Text(
          label,
          style: TextStyle(fontSize: 12.sp),
        ),
      ],
    );
  }

  Widget _buildDoctorCard(DoctorModel doctor) {
    return GestureDetector(
      child: Container(
        key: Key('doctorCard'),
        padding: EdgeInsets.all(12.w),
        margin: EdgeInsets.symmetric(vertical: 4),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(8.r),
        ),
        child: Row(
          children: [
            ClipRRect(
              borderRadius: BorderRadius.circular(28.r),
              child: CachedNetworkImage(
                imageUrl: doctor.photo??'',
                imageBuilder: (context, image) => CircleAvatar(
                  foregroundImage: image,
                  radius: 28,
                ),
                errorWidget: (context, url, error) => Container(
                  decoration: BoxDecoration(
                    color: Colors.black.withOpacity(0.5),
                    borderRadius: BorderRadius.circular(18),
                  ),
                ),
                placeholder: (context, url) => Container(
                  decoration: BoxDecoration(
                    color: const Color(0xffF1F1F1),
                    borderRadius: BorderRadius.circular(18),
                  ),
                ),
              ),
            ),
            SizedBox(width: 12.w),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    doctor.nickName??'',
                    style: TextStyle(
                      fontSize: 16.sp,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  SizedBox(height: 4.h),
                  Text(
                    doctor.title??'',
                    style: TextStyle(
                      fontSize: 12.sp,
                      color: Colors.grey[600],
                    ),
                  ),
                ],
              ),
            ),
            // ElevatedButton(
            //   onPressed: () {},
            //   style: ElevatedButton.styleFrom(
            //     backgroundColor: Colors.blue[600],
            //     shape: RoundedRectangleBorder(
            //       borderRadius: BorderRadius.circular(16.r),
            //     ),
            //   ),
            //   child: Text(
            //     '问诊',
            //     style: TextStyle(color: Colors.white),
            //   ),
            // ),
          ],
        ),
      ),
    );
  }

  Widget _buildArticleCard(ArticleModel article) {
    return GestureDetector(
      onTap: () {
        Get.toNamed(Routes.NEWS_DETAIL, arguments: article);
      },
      child: Container(
        key: Key('articleCard'),
        padding: EdgeInsets.all(12.w),
        margin: EdgeInsets.symmetric(vertical: 4),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(8.r),
        ),
        child: Row(
          children: [
            SizedBox(width: 12.w),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    article.title??'',
                    style: TextStyle(
                      fontSize: 16.sp,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  SizedBox(height: 4.h),
                  Text(
                    article.content??'',
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      fontSize: 12.sp,
                      color: Colors.grey[600],

                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
